<template>
  <div>
    <Card>
      <ButtonGroup style="margin-bottom: 15px;">
        <Add v-model="isOpenAddPage" @addRow="addLkyEvaluation"/>
        <Edit v-model="isOpenEditPage" @updateRow="updateLkyAssessment" :lkyAssessment="editLkyAssessment"/>
      </ButtonGroup>
      <Table
        ref="tableLkyAssessment"
        :data="tableData"
        :columns="columns"
        @on-selection-change="onSelectionChange"
        @on-sort-change="onSortChange"
      />
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page
            :total="total"
            :current="current"
            :page-size="pageSize"
            @on-page-size-change="changePageSize"
            @on-change="changePage"
            :show-total="true"
            show-sizer
          ></Page>
        </div>
      </div>

    </Card>
  </div>
</template>

<script>
import { fileCount} from '@/api/lky/assessment'
import { createFormObj, getToken } from '@/libs/util'

// import Add from './add.vue'
import Edit from './edit.vue'

export default {
  name: 'lkyAssessment',
  components: {
    // Add,
    Edit
  },
  data () {
    return {
      formLkyAss: {
        id: '',
        training_project: '',
        start_time: '',
        end_time: '',
        training_site: '',
        guidance_teacher: '',
        score: ''
        // creation_time:'',
        // update_time:''
      },
      columns: [

        {title: '实训项目', key: 'training_project'},
        { key: 'start_time', title: '开始时间', sortable: true },
        { key: 'end_time', title: '结束时间', sortable: true },
        {title: '实训地点', key: 'training_site'},
        {title: '指导老师', key: 'guidance_teacher'},
        { key: 'score', title: '分值', sortable: true },
        // {title: '创建时间',key: 'creation_time'},
        // {title: '更新时间',key: 'update_time'},
        {
          title: '操作',
          key: 'action',
          width: 250,
          align: 'center',
          render: (h, params) => {
            return h('div', [

              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editLkyAssessment = params.row
                      this.handleOpenPage('edit')
                    }
                  }
                },
                '编辑'
              )

            ])
          }
        }
      ],
      tableData: [],
      total: 0,
      current: 1,
      pageSize: 10,
      isOpenEditPage: false,
      editLkyAssessment: {}
    }
  },
  methods: {
    handleSubmit (name) {
      // 查询按钮事件
      this.getLkyEvaluationList(this.current, this.pageSize)
      this.$Message.success('Success!')
    },
    // handleReset (name) {
    //   // 重置按钮事件
    //   this.$refs[name].resetFields()
    // },
    handleOpenPage (name) {
      // 打开页面事件(添加页,编辑页...)
      if (name === 'add') this.isOpenAddPage = true
      else if (name === 'edit') {
        this.isOpenEditPage = true
      }
    },
	 // onRowClick (row) {
	 //    this.id = row.id
	 //  },
    addLkyEvaluation (row) {
      this.tableData.push(row)
    },
    updateLkyAssessment (row) {
      this.getLkyEvaluationList(this.current, this.pageSize)
      this.$set(this.tableData, row._index, row)
    },

    getLkyEvaluationList (current, pageSize, paras) {
      let para = { pageNumber: current, pageSize: pageSize }
      if (paras) Object.assign(para, paras)
	  Object.assign(para, { sid: this.$route.query.id, uid: getToken() })
      fileCount(this.handleRequestParams(para)).then(res => {
        this.tableData = res.list
        this.current = parseInt(res.pageNumber)
        this.pageSize = parseInt(res.pageSize)
        this.total = parseInt(res.totalRow)
      })
    },
    handleRequestParams (para) {
      Object.assign(para, createFormObj(this.formLkyAss, 'lkyEvaluation'))
      return para
    },
    exportExcel () {
      this.$refs.tableLkyEvaluation.exportCsv({
        filename: `table-${new Date().valueOf()}.csv`
      })
    },
    changePage (pageNumber) {
      this.getLkyEvaluationList(pageNumber, this.pageSize)
    },
    changePageSize (pageSize) {
      this.getLkyEvaluationList(this.current, pageSize)
    },
    remove (index) {
      this.data6.splice(index, 1)
    },
    onSelectionChange (selection) {
      this.selection = selection
    },
    onSortChange (obj) {
      if (obj.order === 'normal') {
        this.getLkyEvaluationList(1, this.pageSize)
      } else {
        this.getLkyEvaluationList(1, this.pageSize, {
          field: obj.key,
          order: obj.order
        })
      }
    }
  },
  mounted () {
	    this.getLkyEvaluationList(this.current, this.pageSize)
  }
}
</script>
